<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="所属工厂" prop="factoryId">
        <el-select
          v-model="queryParams.factoryId"
          placeholder="请选择所属工厂"
          clearable
          size="small"
        >
          <el-option
            v-for="dept in deptOptions"
            :key="dept.deptId"
            :label="dept.name"
            :value="dept.deptId"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="记录编号" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入养护记录编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="记录名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入养护记录名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="养护级别" prop="level">
        <el-select
          v-model="queryParams.level"
          placeholder="请选择养护级别"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in levelOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="养护周期" prop="cycle">
        <el-select
          v-model="queryParams.cycle"
          placeholder="请选择养护周期"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in cycleOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <!--
      <el-form-item label="创建时间">
        <el-date-picker :editable="false" v-model="daterangeTime" size="small" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>-->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="processList"
      @selection-change="handleSelectionChange"
      max-height="550"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="所属工厂" align="center" prop="factoryName" />
      <el-table-column
        label="养护设备"
        align="center"
        prop="deviceName"
        minWidth="80px"
      />
      <el-table-column
        label="养护记录编号"
        align="center"
        prop="code"
        minWidth="100px"
      />
      <el-table-column
        label="养护记录名称"
        align="center"
        prop="name"
        minWidth="200px"
      />
      <el-table-column
        label="所属计划"
        align="center"
        prop="planName"
        minWidth="140px"
      />
      <el-table-column
        label="养护级别"
        align="center"
        prop="level"
        :formatter="levelFormat"
      />
      <el-table-column
        label="养护周期"
        align="center"
        prop="cycle"
        :formatter="cycleFormat"
        width="80"
      />
      <el-table-column
        label="养护人"
        align="center"
        prop="maintainPersonName"
        width="70"
      />
      <!--      <el-table-column label="创建时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d} {h}:{i}") }}</span>
        </template>
      </el-table-column>-->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            >查看</el-button
          >
          <el-button
            v-if="scope.row.status == '02' && scope.row.level == '01'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleReview(scope.row)"
            v-hasPermi="['equipment:processCheck:review']"
            >检查</el-button
          >
          <el-button
            v-if="scope.row.status == '02' && scope.row.level != '01'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleReview(scope.row)"
            v-hasPermi="['equipment:processCheck:review']"
            >复核</el-button
          >
          <el-button
            v-if="scope.row.status == '03' && scope.row.level != '01'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleCheck(scope.row)"
            v-hasPermi="['equipment:processCheck:check']"
            >验收</el-button
          >
          <el-button
            v-if="scope.row.level == '03' && scope.row.status == '04'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleLeader(scope.row)"
            v-hasPermi="['equipment:processCheck:leaderCheck']"
            >负责人审批</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改养护过程对话框 -->
    <el-dialog
      fullscreen
      :title="title"
      :visible.sync="open"
      width="700px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="记录编号" prop="code">
              <el-input
                v-model="form.code"
                placeholder="请输入养护记录编号"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="记录名称" prop="name">
              <el-input
                v-model="form.name"
                placeholder="请输入养护记录名称"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属计划" prop="planId">
              <Dialog
                @dialogChange="form.planId = $event.value"
                :echoValue="form.planName"
                parameter="rows"
                value="id"
                label="name"
                search="code/name"
                url="/equipment/maintainPlan/list"
                :column="['计划编号/code', '计划名称/name']"
                :openDisabled="true"
                :multiple="true"
                placeholder="请选择内容"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="所属工厂" prop="factoryId">
              <el-select
                v-model="form.factoryId"
                placeholder="请选择所属工厂"
                clearable
                size="small"
                :disabled="true"
              >
                <el-option
                  v-for="dept in deptOptions"
                  :key="dept.deptId"
                  :label="dept.name"
                  :value="dept.deptId"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="养护设备" prop="maintainDevice">
              <Dialog
                @dialogChange="form.maintainDevice = $event.value"
                :echoValue="form.deviceName"
                parameter="rows"
                value="id"
                label="name"
                search="code/name"
                url="/basic/eqpInfo/list"
                :column="['设备编号/code', '设备名/name']"
                :openDisabled="true"
                :multiple="true"
                placeholder="请选择内容"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="养护周期" prop="cycle">
              <el-select
                v-model="form.cycle"
                placeholder="请选择养护周期"
                :disabled="true"
              >
                <el-option
                  v-for="dict in cycleOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="养护级别" prop="level">
              <el-select
                v-model="form.level"
                placeholder="请选择养护级别"
                :disabled="true"
              >
                <el-option
                  v-for="dict in levelOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="养护人" prop="maintainPerson">
              <el-input
                v-model="form.maintainPersonName"
                placeholder="请输入养护人"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="养护时间" prop="maintainTime">
              <el-date-picker
                :editable="false"
                clearable
                size="small"
                v-model="form.maintainTime"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择养护时间"
                :disabled="true"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否需要备件" prop="isSpareParts">
              <el-radio-group v-model="form.isSpareParts" :disabled="true">
                <el-radio
                  v-for="dict in isSparePartsStatus"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                  >{{ dict.dictLabel }}</el-radio
                >
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.isSpareParts == 'Y'">
            <el-form-item label="申请备件记录" prop="outStockId">
              <Dialog
                @dialogChange="form.outStockId = $event.value"
                :echoValue="form.outStockName"
                parameter="rows"
                value="id"
                label="name"
                search="code/name"
                url="/equipment/inOutRecord/list"
                :column="['记录编号/code', '记录名称/name']"
                :openDisabled="false"
                placeholder="请选择养护设备"
                :axiosData="{ inOut: 'out' }"
              />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="创建时间" prop="createTime">
              <el-date-picker
                :editable="false"
                clearable
                size="small"
                v-model="form.createTime"
                type="datetime"
                value-format="yyyy-MM-dd HH:mm"
                format="yyyy-MM-dd HH:mm"
                :disabled="true"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>-->
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="form.remark"
                type="textarea"
                placeholder="请输入内容"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider content-position="center">养护过程内容信息</el-divider>
        <!--        <el-row :gutter="10" class="mb8">-->
        <!--          <el-col :span="1.5">-->
        <!--            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddEqpMaintainProcessContent" :disabled="openDisabled">添加</el-button>-->
        <!--          </el-col>-->
        <!--          <el-col :span="1.5">-->
        <!--            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteEqpMaintainProcessContent" :disabled="openDisabled">删除</el-button>-->
        <!--          </el-col>-->
        <!--        </el-row>-->
        <el-table
          :data="eqpMaintainProcessContentList"
          :row-class-name="rowEqpMaintainProcessContentIndex"
          @selection-change="handleEqpMaintainProcessContentSelectionChange"
          ref="eqpMaintainProcessContent"
        >
          <!--          <el-table-column type="selection" width="50" align="center" />-->
          <el-table-column
            label="序号"
            align="center"
            prop="index"
            width="50"
            fixed
          />
          <el-table-column label="项目" prop="itemName">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.itemName"
                placeholder="请输入项目"
                :disabled="true"
              />
            </template>
          </el-table-column>
          <el-table-column label="保养内容" prop="maintainContent">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.maintainContent"
                placeholder="请输入保养内容"
                :disabled="true"
              />
            </template>
          </el-table-column>
          <el-table-column label="保养情况" prop="maintainResult">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.maintainResult"
                placeholder="请输入保养情况"
                :disabled="true"
              />
            </template>
          </el-table-column>
          <el-table-column label="养护人" prop="createName">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.createName"
                placeholder=""
                :disabled="true"
              />
            </template>
          </el-table-column>
          <el-table-column label="养护时间" prop="createTime">
            <template slot-scope="scope">
              <el-time-picker
                :editable="false"
                clearable
                size="small"
                v-model="scope.row.createTime"
                type="datetime"
                value-format="HH:mm"
                placeholder=""
                :disabled="true"
              >
              </el-time-picker>
            </template>
          </el-table-column>
          <el-table-column label="复核通过" prop="checkResult">
            <template slot-scope="scope">
              <el-radio-group
                v-model="scope.row.checkResult"
                :disabled="showStatus != '02'"
                @change="setUserInfo(scope.row)"
              >
                <el-radio
                  v-for="dict in checkResultOptions"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                  >{{ dict.dictLabel }}</el-radio
                >
              </el-radio-group>
            </template>
          </el-table-column>
          <el-table-column label="检查人" prop="checkUser">
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.checkUserName"
                placeholder=""
                :disabled="true"
              />
            </template>
          </el-table-column>
          <template v-if="levelst != '01'">
            <el-table-column label="检查时间" prop="checkTime">
              <template slot-scope="scope">
                <el-date-picker
                  clearable
                  size="small"
                  v-model="scope.row.checkTime"
                  type="datetime"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  placeholder=""
                >
                </el-date-picker>
              </template>

              <!-- <template slot-scope="scope">
              <el-date-picker :editable="false" clearable size="small" v-model="scope.row.checkTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="" :disabled="true">
              </el-date-picker>
            </template> -->
            </el-table-column>
          </template>
          <!--          <el-table-column label="检查时间" prop="checkTime">-->
          <!--            <template slot-scope="scope">-->
          <!--              <el-date-picker-->
          <!--                :editable="false"-->
          <!--                clearable-->
          <!--                size="small"-->
          <!--                v-model="scope.row.checkTime"-->
          <!--                type="datetime"-->
          <!--                value-format="yyyy-MM-dd HH:mm:ss"-->
          <!--                placeholder="选择检查时间"-->
          <!--                :disabled="true"-->
          <!--              >-->
          <!--              </el-date-picker>-->
          <!--            </template>-->
          <!--          </el-table-column>-->
          <!--          <el-table-column label="备注" prop="remark">-->
          <!--            <template slot-scope="scope">-->
          <!--              <el-input v-model="scope.row.remark" type="textarea" placeholder="请输入内容" :disabled="openDisabled"/>-->
          <!--            </template>-->
          <!--          </el-table-column>-->
        </el-table>
        <el-divider
          content-position="center"
          v-if="['03', '04', '05'].includes(showStatus) || openDisabled"
          >养护验收信息</el-divider
        >
        <el-row>
          <template v-if="levelst == '01'">
            <el-col :span="12">
              <el-form-item label="检查意见" prop="oneCheckOpinion">
                <el-input
                  v-model="form.oneCheckOpinion"
                  placeholder="请输入意见"
                />
              </el-form-item>
            </el-col>
          </template>

          <el-col
            :span="12"
            v-if="['03', '04', '05'].includes(showStatus) && !openDisabled"
          >
            <el-col :span="12">
              <el-form-item label="验收结果" prop="status">
                <el-radio-group
                  v-model="form.status"
                  :disabled="showStatus != '03' || openDisabled"
                >
                  <el-radio
                    v-for="dict in statusChecks"
                    :key="dict.dictValue"
                    :label="dict.dictValue"
                    >{{ dict.dictLabel }}</el-radio
                  >
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="验收时间" prop="checkOpinion">
                <el-date-picker
                  clearable
                  size="small"
                  v-model="form.checkTime"
                  type="date"
                  value-format="yyyy-MM-dd"
                  placeholder="选择验收时间"
                >
                </el-date-picker>
              </el-form-item>

              <!-- <el-form-item label="验收时间" prop="checkTime">
                <el-date-picker :editable="false" clearable size="small" v-model="form.checkTime" type="date" value-format="yyyy-MM-dd" placeholder="选择验收时间" :disabled="true">
                </el-date-picker>
              </el-form-item> -->
            </el-col>
          </el-col>
          <template v-if="showStatus == '04'">
            <el-col :span="12">
              <el-form-item label="验收人" prop="checkUserName">
                <el-input v-model="form.checkUserName" :disabled="true" />
              </el-form-item>
            </el-col>
            <!--            <el-col :span="12">
              <el-form-item label="验收时间" prop="checkTime">
                <el-date-picker :editable="false" clearable size="small" v-model="form.checkTime" type="date" value-format="yyyy-MM-dd" placeholder="选择验收时间" :disabled="true">
                </el-date-picker>
              </el-form-item>
            </el-col>-->
          </template>
          <el-col :span="24" v-if="['03', '04', '05'].includes(showStatus)">
            <el-form-item label="验收意见" prop="checkOpinion">
              <el-input
                v-model="form.checkOpinion"
                placeholder="请输入验收意见"
                :disabled="showStatus != '03' || openDisabled"
              />
            </el-form-item>
          </el-col>
          <template v-if="showStatus == '04'">
            <el-col :span="12" v-if="!openDisabled">
              <el-form-item label="审批结果" prop="status">
                <el-radio-group
                  v-model="form.status"
                  :disabled="showStatus != '04'"
                >
                  <el-radio
                    v-for="dict in statusLeaders"
                    :key="dict.dictValue"
                    :label="dict.dictValue"
                    >{{ dict.dictLabel }}</el-radio
                  >
                </el-radio-group>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item
                label="审批意见"
                prop="leaderCheckOpinion"
                v-if="showStatus == '04'"
              >
                <el-input
                  v-model="form.leaderCheckOpinion"
                  placeholder="请输入部门负责人审批意见"
                  :disabled="showStatus != '04'"
                />
              </el-form-item>
            </el-col>
          </template>
          <el-col :span="12" v-if="openDisabled">
            <el-form-item label="养护状态" prop="status">
              <el-select
                v-model="form.status"
                placeholder="请选择养护级别"
                :disabled="true"
              >
                <el-option
                  v-for="dict in statusOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!openDisabled">
        <el-button
          type="primary"
          @click="submitForm"
          v-show="showStatus == '02' && levelst == '01'"
          >检查</el-button
        >
        <el-button
          type="primary"
          @click="submitForm"
          v-show="showStatus == '02' && levelst != '01'"
          >复核</el-button
        >
        <el-button
          type="primary"
          @click="submitForm"
          v-show="showStatus == '03' && levelst != '01'"
          >验收</el-button
        >
        <el-button
          type="primary"
          @click="submitForm"
          v-show="showStatus == '04' && levelst != '01'"
          >审批</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listProcessCheck,
  getProcessCheck,
  updateProcessReview,
  updateProcessCheck,
  updateProcessLeaderCheck,
} from "@/api/equipment/maintain/processCheck";
import { getFactoryList } from "@/api/equipment/common";
import { treeselect } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "Process",
  components: { Treeselect },
  data() {
    return {
      showStatus: "05",
      levelst: "01",
      buttonStatus: 0,
      //弹窗是否禁止编辑
      openDisabled: true,
      //验收模块
      isShowCheck: false,
      isDisabledCheck: false,
      //负责人审批
      isShowLeader: false,
      isDisabledLeader: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedEqpMaintainProcessContent: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 养护过程表格数据
      processList: [],
      // 养护过程内容表格数据
      eqpMaintainProcessContentList: [],
      // 检查结果字典
      checkResultOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 养护级别字典
      levelOptions: [],
      // 养护周期字典
      cycleOptions: [],
      // 养护状态字典
      statusOptions: [],
      daterangeTime: [],
      isSparePartsStatus: [],
      // 部门树选项
      deptOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
        code: null,
        name: null,
        planId: null,
        planIds: null,
        level: null,
        cycle: null,
        status: null,
        maintainPerson: null,
        maintainTime: null,
        factoryId: null,
        maintainDevice: null,
        startTime: null,
        endTime: null,
      },
      statusChecks: [
        { dictLabel: "验收通过", dictValue: "04" },
        { dictLabel: "验收未通过", dictValue: "01" },
      ],
      statusLeaders: [
        { dictLabel: "审批通过", dictValue: "05" },
        { dictLabel: "审批未通过", dictValue: "01" },
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
    this.getDicts("maintain_level").then((response) => {
      this.levelOptions = response.data;
    });
    this.getDicts("maintain_cycle").then((response) => {
      this.cycleOptions = response.data;
    });
    this.getDicts("maintain_status").then((response) => {
      this.statusOptions = response.data;
    });
    this.getDicts("sys_yes_no").then((response) => {
      this.checkResultOptions = response.data;
      this.isSparePartsStatus = response.data;
    });
    // this.getTreeselect();
    this.getFactory();
  },
  methods: {
    /** 查询部门下拉树结构 */
    getFactory() {
      getFactoryList().then((response) => {
        this.deptOptions = response.data;
      });
    },
    // /** 查询部门下拉树结构 */
    // getTreeselect() {
    //     treeselect().then((response) => {
    //         this.deptOptions = response.data;
    //     });
    // },
    /** 查询养护过程列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeTime && "" != this.daterangeTime) {
        this.queryParams.params["beginTime"] = this.daterangeTime[0];
        this.queryParams.params["endTime"] = this.daterangeTime[1];
      }
      listProcessCheck(this.queryParams).then((response) => {
        this.processList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 养护级别字典翻译
    levelFormat(row, column) {
      return this.selectDictLabel(this.levelOptions, row.level);
    },
    // 养护周期字典翻译
    cycleFormat(row, column) {
      return this.selectDictLabel(this.cycleOptions, row.cycle);
    },
    // 养护状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    //验收控制
    checkShow(isShow, isDisabled) {
      this.isShowCheck = isShow;
      this.isDisabledCheck = isDisabled;
    },
    leaderShow(isShow, isDisabled) {
      this.isShowLeader = isShow;
      this.isDisabledLeader = isDisabled;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        name: null,
        planId: null,
        level: null,
        maintainType: null,
        cycle: null,
        status: null,
        maintainPerson: null,
        maintainTime: null,
        factoryId: null,
        maintainDevice: null,
        startTime: null,
        endTime: null,
        checkUser: null,
        checkTime: null,
        checkOpinion: null,
        leaderCheckName: null,
        leaderCheckTime: null,
        leaderCheckOpinion: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
        remark: null,
        planName: null,
        oneCheckOpinion: null,
      };
      this.eqpMaintainProcessContentList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.openDisabled = false;
      this.open = true;
      this.title = "添加养护过程";
    },
    /** 复核 */
    handleReview(row) {
      this.reset();
      this.openDisabled = false;
      this.buttonStatus = 1;
      // this.checkShow(false,true);
      // this.leaderShow(false,true);
      const id = row.id || this.ids;
      getProcessCheck(id).then((response) => {
        this.form = response.data;
        this.showStatus = this.form.status;
        this.levelst = this.form.level;
        this.eqpMaintainProcessContentList =
          response.data.eqpMaintainProcessContentList;
        this.open = true;
        if (this.levels == "01") {
          this.title = "检查";
        } else {
          this.title = "复查";
        }
      });
    },
    setUserInfo(data) {
      if (data.index == 1) {
        for (
          let i = 0;
          i < this.form.eqpMaintainProcessContentList.length;
          i++
        ) {
          this.form.eqpMaintainProcessContentList[i].checkResult =
            data.checkResult;
          this.form.eqpMaintainProcessContentList[i].checkUser =
            this.$store.state.user.name;
          this.form.eqpMaintainProcessContentList[i].checkUserName =
            this.$store.state.user.nickName;
          this.form.eqpMaintainProcessContentList[i].checkTime = this.parseTime(
            new Date()
          );
        }
      }
      data.checkUserName = this.$store.state.user.nickName;
      data.checkUser = this.$store.state.user.name;
      data.checkTime = this.parseTime(new Date());
    },
    /** 验收 */
    handleCheck(row) {
      this.reset();
      this.openDisabled = false;
      this.buttonStatus = 2;
      const id = row.id || this.ids;
      getProcessCheck(id).then((response) => {
        this.form = response.data;
        this.showStatus = this.form.status;
        this.form.status = "04";
        this.levelst = this.form.level;
        this.eqpMaintainProcessContentList =
          response.data.eqpMaintainProcessContentList;
        this.open = true;
        this.title = "验收";
        var tashDtaSTr = null;
        if (this.form.checkTime == null) {
          for (
            let i = 0;
            i < this.form.eqpMaintainProcessContentList.length;
            i++
          ) {
            if (this.form.eqpMaintainProcessContentList[i].checkTime != null) {
              // console.log(this.form.eqpMaintainProcessContentList[i].checkTime);
              var tashDta = new Date(tashDtaSTr).getTime();
              if (tashDta != null) {
                var createDate = new Date(
                  this.form.eqpMaintainProcessContentList[i].checkTime
                ).getTime();

                if (createDate - tashDta > 0) {
                  tashDtaSTr =
                    this.form.eqpMaintainProcessContentList[i].checkTime;
                }
              } else {
                tashDtaSTr =
                  this.form.eqpMaintainProcessContentList[i].checkTime;
              }
            }
          }
          this.form.checkTime = tashDtaSTr;
          // console.log(tashDtaSTr);
        }
      });
    },
    /** 审批*/
    handleLeader(row) {
      this.reset();
      this.openDisabled = false;
      this.buttonStatus = 3;
      // this.checkShow(true,true);
      // this.leaderShow(true,false);
      const id = row.id || this.ids;
      getProcessCheck(id).then((response) => {
        this.form = response.data;
        this.showStatus = this.form.status;
        this.form.status = "05";
        this.levelst = this.form.level;
        this.eqpMaintainProcessContentList =
          response.data.eqpMaintainProcessContentList;
        this.open = true;
        this.title = "负责人审批";
      });
    },
    /** 查看按钮 */
    handleView(row) {
      this.reset();
      this.openDisabled = true;
      this.checkShow(false, false);
      this.leaderShow(false, false);
      const id = row.id || this.ids;
      getProcessCheck(id).then((response) => {
        this.form = response.data;
        this.showStatus = this.form.status;
        this.levelst = this.form.level;
        this.eqpMaintainProcessContentList =
          response.data.eqpMaintainProcessContentList;
        this.open = true;
        this.title = "查看养护过程";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.eqpMaintainProcessContentList =
            this.eqpMaintainProcessContentList;
          if (this.form.id != null) {
            if (this.buttonStatus == 1) {
              //未复核
              let subCode = true;
              let subCodeResult = "";
              // 复核未通过
              let statusCode = true;
              let statusCodeResult = "";
              for (
                let i = 0;
                i < this.form.eqpMaintainProcessContentList.length;
                i++
              ) {
                if (
                  this.form.eqpMaintainProcessContentList[i].checkResult == null
                ) {
                  subCode = false;
                  subCodeResult +=
                    this.form.eqpMaintainProcessContentList[i].itemName + ",";
                }
                if (
                  this.form.eqpMaintainProcessContentList[i].checkResult == "N"
                ) {
                  statusCode = false;
                  statusCodeResult +=
                    this.form.eqpMaintainProcessContentList[i].itemName + ",";
                }
              }
              if (subCode) {
                if (statusCode) {
                  this.form.status = "03";
                  const result =
                    "复核结果为【复核通过】,提交后将无法修改，是否确认提交？";
                  this.$confirm(result, "警告", {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "error",
                  })
                    .then(() => {
                      return updateProcessReview(this.form);
                    })
                    .then(() => {
                      this.msgSuccess("复核成功");
                      this.open = false;
                      this.getList();
                    })
                    .catch(() => {});
                } else {
                  this.$confirm(
                    "存在复核未通过养护项目【" +
                      statusCodeResult.substring(
                        0,
                        statusCodeResult.length - 1
                      ) +
                      "】，将回退给养护人员，是否继续提交?",
                    "警告",
                    {
                      confirmButtonText: "确定",
                      cancelButtonText: "取消",
                      type: "warning",
                    }
                  )
                    .then(function () {
                      updateProcessReview(this.form).then((response) => {
                        this.msgSuccess("回退成功");
                        this.open = false;
                        this.getList();
                      });
                    })
                    .catch(() => {});
                }
              } else {
                this.$confirm(
                  "存在未复核的养护项目【" +
                    subCodeResult.substring(0, subCodeResult.length - 1) +
                    "】！",
                  "警告",
                  {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                  }
                )
                  .then(function () {
                    updateProcessReview(this.form).then((response) => {
                      this.msgSuccess("保存成功");
                      this.open = false;
                      this.getList();
                    });
                  })
                  .catch(() => {});
              }
            } else if (this.buttonStatus == 2) {
              const result =
                this.form.status == "04" ? "验收通过" : "验收未通过";
              this.$confirm(
                "验收结果为【" + result + "],提交后将无法修改，是否确认提交？",
                "警告",
                {
                  confirmButtonText: "确定",
                  cancelButtonText: "取消",
                  type: "error",
                }
              )
                .then(() => {
                  return updateProcessCheck(this.form);
                })
                .then(() => {
                  this.msgSuccess("验收成功");
                  this.open = false;
                  this.getList();
                })
                .catch(() => {});
            } else if (this.buttonStatus == 3) {
              const result =
                this.form.status == "05" ? "审批通过" : "审批未通过";
              this.$confirm(
                "审批结果为【" + result + "],提交后将无法修改，是否确认提交？",
                "警告",
                {
                  confirmButtonText: "确定",
                  cancelButtonText: "取消",
                  type: "error",
                }
              )
                .then(() => {
                  return updateProcessLeaderCheck(this.form);
                })
                .then(() => {
                  this.msgSuccess("负责人审批完成");
                  this.open = false;
                  this.getList();
                })
                .catch(() => {});
              // updateProcessLeaderCheck(this.form).then((response) => {
              //   this.msgSuccess("负责人审批成功");
              //   this.open = false;
              //   this.getList();
              // });
            }
          } else {
            addProcessCheck(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 养护过程内容序号 */
    rowEqpMaintainProcessContentIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 养护过程内容添加按钮操作 */
    handleAddEqpMaintainProcessContent() {
      let obj = {};
      obj.itemName = "";
      obj.maintainContent = "";
      obj.maintainResult = "";
      obj.checkResult = "";
      obj.checkUser = "";
      obj.checkUserName = "";
      obj.checkTime = "";
      obj.remark = "";
      this.eqpMaintainProcessContentList.push(obj);
    },
    /** 养护过程内容删除按钮操作 */
    handleDeleteEqpMaintainProcessContent() {
      if (this.checkedEqpMaintainProcessContent.length == 0) {
        this.$alert("请先选择要删除的养护过程内容数据", "提示", {
          confirmButtonText: "确定",
        });
      } else {
        this.eqpMaintainProcessContentList.splice(
          this.checkedEqpMaintainProcessContent[0].index - 1,
          1
        );
      }
    },
    /** 单选框选中数据 */
    handleEqpMaintainProcessContentSelectionChange(selection) {
      if (selection.length > 1) {
        this.$refs.eqpMaintainProcessContent.clearSelection();
        this.$refs.eqpMaintainProcessContent.toggleRowSelection(
          selection.pop()
        );
      } else {
        this.checkedEqpMaintainProcessContent = selection;
      }
    },
  },
};
</script>
